﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace BaseThing.Engine
{
    class Particle
    {
        private double time;
        private double lastTime;

        public int id;

        public Texture2D texture;

        public Color color = Color.White;
        public Vector2 position = Vector2.Zero;
        public Vector2 velocity = Vector2.Zero;
        public Vector2 acceleration = Vector2.Zero;
        public Vector2 rotation = Vector2.Zero;
        public Vector2 origin = Vector2.Zero;

        int tileWidth = 0;
        int tileHeight = 0;

        public float totalLife;
        public float life;

        public float alpha;
        public float size;

        public float bounciness;

        public bool active;

        public float angle;

        public float speed;

        public Particle(int ID, Texture2D texture)
        {
            id = ID;

            totalLife = 1.0f;
            life = 1.0f;

            alpha = 1.0f;
            size = 1.0f;
            angle = 0.0f;
            speed = 1.0f;

            bounciness = 0.9f;

            active = true;

            time = 0;
            lastTime = -1;

            this.texture = texture;

            tileWidth = texture.Width;
            tileHeight = texture.Height;

            origin = new Vector2(tileWidth / 2, tileHeight / 2);
        }

        private Vector2 Rotation(float angle, float x, float y)
        {
            float finalX = (float)(x * Math.Cos(angle) - y * Math.Sin(angle));
            float finalY = (float)(y * Math.Cos(angle) + x * Math.Sin(angle));

            x = finalX;
            y = finalY;

            return new Vector2(x, y);
        }

        public void Update(GameTime gameTime)
	    {
            time += gameTime.ElapsedGameTime.TotalMilliseconds;

		    if ( active == false )
			    return;

		    if ( lastTime == -1 )
			    lastTime = time;

		    float change = (float) ( time -lastTime ) / 1000.0f ;
            change = change * speed;
                    
		    velocity = velocity + (acceleration *change);
		    position = position + (velocity * change);

            Vector2 finalRot  = Rotation(rotation.Y * (float)time / 1000.0f, position.X, 0.0f) + Rotation(rotation.X * (float)time / 1000.0f, position.Y, 0.0f);

            angle = (float)Math.Atan2((double)finalRot.Y, (double)finalRot.X);

		    if ( position.Y < 0.0f)
		    {
			    velocity.Y = velocity.Y * -bounciness;
			    position.Y = 0.0f;

		    }
		 
		    life -= change;

            if (life <= 0.0f)
            {
                active = false;
            }

		    lastTime = time;
	    }

        public void Draw(SpriteBatch spriteBatch)
        {

            if (!active) return;

            const float fadeTime = 0.2f;

            float finalAlpha = 0;

            if (totalLife - life < fadeTime)
                finalAlpha = (totalLife - life) / fadeTime * alpha;
            else if (life < 1.0f)
                    finalAlpha = life * alpha;
            else
                finalAlpha =  alpha;
               
            spriteBatch.Draw(texture, position, GetBounds(), new Color(color.R, color.G, color.B) * finalAlpha, angle, origin, size, SpriteEffects.None, 0f);
         }

        public Rectangle GetBounds()
        {   
            return new Rectangle(0, 0, tileWidth, tileHeight);
        }

    }
}
